Ext.define('Intranet.Desktop.controller.InstalledModules', {
    
    extend: 'Ext.app.Controller',
    
    views: ['Intranet.Desktop.view.InstalledModules'],
    
    init: function(){
        this.control({
            'panel button[id=installedDisableBtn]' : {
                click : function(){
                    var installedView = Ext.getCmp('installedView');
                    var selectionModel = installedView.getSelectionModel();
                    
                    if(selectionModel.getCount())
                        {
                            var selectedRecord = selectionModel.getSelection()[0];

                            Ext.Ajax.request({
                                
                                url: '/src/public/module/modulesusers/disable',

                                params: {
                                    id: selectedRecord.data.mid
                                    
                                },
                                
                                method: 'POST',
                                
                                success: function(response){
                                    var data = Ext.decode(response.responseText);
                                    
                                    //bind refresh store to view
                                    var installedStore = Ext.create('Intranet.Desktop.store.InstalledModulesStore');
                                    installedView.bindStore(installedStore);

                                    loadUserModules();
                                    Ext.Msg.alert('Disable', data.msg);
                                }
                            });
                        }
                        else
                        {
                            Ext.Msg.alert('Register', 'Select module to disable!');
                        }
                }
            },
            
            'panel button[id=disabledEnableBtn]' : {
                click : function(){
                    var disabledView = Ext.getCmp('disabledView');
                    var selectionModel = disabledView.getSelectionModel();
                    
                    if(selectionModel.getCount())
                        {
                            var selectedRecord = selectionModel.getSelection()[0];

                            Ext.Ajax.request({
                                
                                url: '/src/public/module/modulesusers/enable',

                                params: {
                                    id: selectedRecord.data.mid
                                    
                                },
                                
                                method: 'POST',
                                
                                success: function(response){
                                    var data = Ext.decode(response.responseText);
                                    
                                    if(data.success === 'true')
                                    {

                                        //bind refresh store to view
                                        var disabledStore = Ext.create('Intranet.Desktop.store.DisabledModulesStore');
                                        disabledView.bindStore(disabledStore);

                                        loadUserModules();
                                        Ext.Msg.alert('Enable', data.msg);
                                    }
                                    else
                                    {
                                        Ext.Msg.alert('Failed', data.msg);
                                    }
                                }
                            });
                        }
                        else
                        {
                            Ext.Msg.alert('Register', 'Select module to disable!');
                        }
                }
            },
            
            'panel button[id=installedRemoveBtn]' : {
                click : function(){
                    var installedView = Ext.getCmp('installedView');
                    var selectionModel = installedView.getSelectionModel();
                    
                    if(selectionModel.getCount())
                        {
                            var selectedRecord = selectionModel.getSelection()[0];

                            Ext.Ajax.request({
                                
                                url: '/src/public/module/modulesusers/unRegister',

                                params: {
                                    id: selectedRecord.data.mid
                                    
                                },
                                
                                method: 'POST',
                                
                                success: function(response){
                                    var data = Ext.decode(response.responseText);
                                    
                                    if(data.success === 'true')
                                    {

                                        //bind refresh store to view
                                        var disabledStore = Ext.create('Intranet.Desktop.store.InstalledModulesStore');
                                        installedView.bindStore(disabledStore);

                                        loadUserModules();
                                        Ext.Msg.alert('Remove', data.msg);
                                    }
                                    else
                                    {
                                        Ext.Msg.alert('Failed', data.msg);
                                    }
                                }
                            });
                        }
                        else
                        {
                            Ext.Msg.alert('Register', 'Select module to disable!');
                        }
                }
            },
            
            'panel button[id=disabledRemoveBtn]' : {
                click : function(){
                    var disabledView = Ext.getCmp('disabledView');
                    var selectionModel = disabledView.getSelectionModel();
                    
                    if(selectionModel.getCount())
                        {
                            var selectedRecord = selectionModel.getSelection()[0];

                            Ext.Ajax.request({
                                
                                url: '/src/public/module/modulesusers/unRegister',

                                params: {
                                    id: selectedRecord.data.mid
                                    
                                },
                                
                                method: 'POST',
                                
                                success: function(response){
                                    var data = Ext.decode(response.responseText);
                                    
                                    if(data.success === 'true')
                                    {

                                        //bind refresh store to view
                                        var disabledStore = Ext.create('Intranet.Desktop.store.DisabledModulesStore');
                                        disabledView.bindStore(disabledStore);

                                        loadUserModules();
                                        Ext.Msg.alert('Remove', data.msg);
                                    }
                                    else
                                    {
                                        Ext.Msg.alert('Failed', data.msg);
                                    }
                                }
                            });
                        }
                        else
                        {
                            Ext.Msg.alert('Register', 'Select module to disable!');
                        }
                }
            },
        });
    }

});

